package com.example.myfragmentdemo;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentManager.OnBackStackChangedListener;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.R;
import com.example.adapter.MessageAdapter;
import com.example.adapter.MyContactExpandableListAdapter;
import com.example.myfragmentdemo.LikeQQActivity.onBackInWebViewListener;
import com.example.myfragmentdemo.MyAlertDialog.OnItemClickListener;
import com.example.myview.MyCustomExpandableListView;
import com.example.popup.MyMessageItemSettingPopupWindow;
import com.example.popup.MyPopupWindow;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshWebView;

@SuppressLint("ValidFragment")
public class LikeQQFragment extends Fragment implements onBackInWebViewListener {
	EnumFragment mFragment;
	
	private PullToRefreshListView pullToRefreshListView = null; 
	private PullToRefreshScrollView pullToRefreshScrollView = null; 
	private PullToRefreshWebView pullToRefreshWebView = null; 
	View messageLayout = null;
	
	public static enum EnumFragment {
	    CONTACT, MESSAGE, NEWS, SETTINGS;
	}
	
	@SuppressLint("ValidFragment")
	public LikeQQFragment(EnumFragment eFragment) {
		super();
		mFragment = eFragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		 
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		switch (mFragment) {
		case CONTACT:
			messageLayout = inflater.inflate(R.layout.contact_layout,
					container, false);
			pullToRefreshScrollView = (PullToRefreshScrollView)messageLayout.findViewById(R.id.refresh_view_contact);
			
			ExpandableListView expandableListView = (ExpandableListView)messageLayout.findViewById(R.id.contact_expandlist);
			expandableListView.setAdapter(new MyContactExpandableListAdapter(getActivity()));

			break;
		case MESSAGE:
			messageLayout = inflater.inflate(R.layout.message_layout,
					container, false);
			final MessageAdapter messageAdapter = new MessageAdapter(getActivity());
			pullToRefreshListView = (PullToRefreshListView)messageLayout.findViewById(R.id.refresh_view_message);
			pullToRefreshListView.setAdapter(messageAdapter);
			pullToRefreshListView
					.setOnRefreshListener(new OnRefreshListener<ListView>() {

						@Override
						public void onRefresh(
								PullToRefreshBase<ListView> refreshView) {
							new AsyncTask<Void, Void, Void>() {

								@Override
								protected Void doInBackground(
										Void... arg0) {
									try {
										Thread.sleep(3000);
									} catch (InterruptedException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
									return null;
								}
								
								protected void onPostExecute(Void result) {
									messageAdapter.updateMessage();
									pullToRefreshListView.onRefreshComplete();
								};

								protected void onPreExecute() {
								};

							}.execute();

						}
					});
			pullToRefreshListView.setOnItemClickListener(messageAdapter);
			pullToRefreshListView.getRefreshableView().setOnItemLongClickListener(new OnItemLongClickListener(){

				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					final int id = (int)arg3;
					final View view = arg1;
					final MyPopupWindow myPopupWindow = new MyPopupWindow(getActivity(),messageAdapter);
					myPopupWindow.showAsDropDown(arg1, getActivity().getWindowManager().getDefaultDisplay().getWidth()/2 - myPopupWindow.getWidth()/2, 0);
					myPopupWindow.setOnItemClickListener(new MyPopupWindow.OnItemClickListener() {

						@Override
						public void onClick(View v) {
							switch (v.getId()) {
							case R.id.popup_delete:
								Toast.makeText(getActivity(), "delete item", Toast.LENGTH_LONG).show();
								System.out.println("popup_delete");
								messageAdapter.deleteItem(id);
								pullToRefreshListView.onRefreshComplete();
								myPopupWindow.dismiss();
								break;
							case R.id.popup_settings:
//								final MyAlertDialog myAlertDialog = new MyAlertDialog(getActivity());
//								myAlertDialog.create().show();
//								myAlertDialog.setOnItemClickListener(new OnItemClickListener() {
//									@Override
//									public void onClick() {
//										//dialog.dismiss();
//									}
//								});
								
								
								MyMessageItemSettingPopupWindow messageItemSettingPopupWindow = new MyMessageItemSettingPopupWindow(getActivity());
								messageItemSettingPopupWindow.showAtLocation(messageLayout.findViewById(R.id.message_main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);

								myPopupWindow.dismiss();
								break;
							default:
								break;
							}
							
						}
						
					});
					return true;
				}
				
			});
			
			break;
		case NEWS:
			messageLayout = inflater.inflate(R.layout.news_layout,
					container, false);
			pullToRefreshWebView = (PullToRefreshWebView) messageLayout.findViewById(R.id.refresh_news_message);
			WebView webView = pullToRefreshWebView.getRefreshableView();
			webView.getSettings().setJavaScriptEnabled(true);
			webView.setWebViewClient(new SampleWebViewClient());
			webView.loadUrl("http://news.qq.com/");
			
			break;
		case SETTINGS:
			messageLayout = inflater.inflate(R.layout.settings_layout,
					container, false);
			break;
		default:
			break;
		}
		
		
		return messageLayout;
	}
	private static class SampleWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
	
	
	@Override
	public void onBack() {
		pullToRefreshWebView.getRefreshableView().goBack();
		
	}
	
	
}
